//
//  DJTableTVIewModel.swift
//  dj-learn-Swift
//
//  Created by Sun Jae on 2022/5/10.
//  Copyright © 2022 S.J. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

class DJTableTVIewModel {
    let firstItem = DJTableItemModel(title: "姓名", value: nil)
    let secondItem = DJTableItemModel(title: "性别", value: nil)
    var items: [DJTableItemModel] = []
    
    let validatedName: Driver<Bool>
    let validatedGender: Driver<Bool>
    // Is signup button enabled
    let signupEnabled: Driver<Bool>
    
    init( input: ( name: Driver<String>, gender: Driver<String>, submitTap: Signal<()> ) ) {
        self.items = [self.firstItem, self.secondItem]
        
        validatedName = input.name.map { name in
            let nameOK = name.count > 4
            print("nameOK: \(nameOK)--- \(name)")
            return nameOK
        }
        
        validatedGender = input.gender.map { gender in
            let genderOK =  gender.count == 2
            print("genderOK: \(genderOK)--- \(gender)")
            return genderOK
        }
        
        signupEnabled = Driver.combineLatest(validatedName, validatedGender) { nameOK, genderOK  in
            let able = nameOK && genderOK
            print(able)
            return able
        }.distinctUntilChanged()
    }
}

class DJTableItemModel {
    var title: String
    var value: String?
    
    init(title: String, value: String?) {
        self.title = title
        self.value = value
    }
}
